package AionCrafter.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableRowSorter;

import AionCrafter.AionCrafterApplication;
import AionCrafter.data.Recipe;
import AionCrafter.data.list.RecipeList;

public class BrowsePanel extends JPanel implements ActionListener, ListSelectionListener, DocumentListener {
	private int mode;
	
	JRadioButton allTypeButton = new JRadioButton("All");
	JRadioButton modeSpec1Button = new JRadioButton();
	JRadioButton modeSpec2Button = new JRadioButton();
	
	JRadioButton allRarityButton = new JRadioButton("All");
	JRadioButton commonRarityButton = new JRadioButton("Common");
	JRadioButton superiorRarityButton = new JRadioButton("Superior");
	JRadioButton heroicRarityButton = new JRadioButton("Heroic");
	JRadioButton fabledRarityButton = new JRadioButton("Fabled");
	
	JList recipeDisplay = new JList();
	JTable recipeTable;
	JTextField searchField = new JTextField();
	Map<String,Recipe> recipeMap;
	RecipeTableModel recipeModel = new RecipeTableModel();
	TableRowSorter<RecipeTableModel> sorter;
	SubComponentsPanel scPanel;

	public void init(int mode) {
		this.removeAll();
		this.mode = mode;
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		//########################
		//Recipe Box
		//########################
		JPanel recipePanel = new JPanel();
		recipePanel.setLayout(new BoxLayout(recipePanel, BoxLayout.PAGE_AXIS));
		
		JPanel searchPanel = new JPanel();
		searchPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		JLabel searchLabel = new JLabel("Search:");
		
		searchField.getDocument().addDocumentListener(this);
		searchField.addActionListener(this);
		searchField.setPreferredSize(new Dimension(200,20));
		searchPanel.add(searchLabel);
		searchPanel.add(searchField);
		
		//Begin JTable implementation
		recipeModel.changeMode(mode);
		sorter = new TableRowSorter<RecipeTableModel>(recipeModel);
		recipeTable = new JTable(recipeModel);
		recipeTable.setRowSorter(sorter);
		recipeTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		recipeTable.getColumnModel().getColumn(0).setPreferredWidth(30);
		recipeTable.getColumnModel().getColumn(1).setPreferredWidth(252);
		recipeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		recipeTable.getSelectionModel().addListSelectionListener(this);
		recipeTable.getColumnModel().setColumnSelectionAllowed(false);
		JScrollPane recipeScroll = new JScrollPane(recipeTable);
		recipeScroll.setPreferredSize(new Dimension(300,120));
		recipePanel.add(searchPanel);
		recipePanel.add(recipeScroll);
		
		
		//########################
		//Rarity Buttons
		//########################
		JPanel rarityPanel = new JPanel();
		rarityPanel.setLayout(new BoxLayout(rarityPanel, BoxLayout.PAGE_AXIS));
		
		allRarityButton.setSelected(true);
		superiorRarityButton.setForeground(new Color(0x41B937)); //green
		heroicRarityButton.setForeground(new Color(0x1499AE)); //blue
		fabledRarityButton.setForeground(new Color(0xE09908)); //gold
		
		ButtonGroup rarityGroup = new ButtonGroup();
		rarityGroup.add(allRarityButton);
		rarityGroup.add(commonRarityButton);
		rarityGroup.add(superiorRarityButton);
		rarityGroup.add(heroicRarityButton);
		rarityGroup.add(fabledRarityButton);
		
		allRarityButton.addActionListener(this);
		commonRarityButton.addActionListener(this);
		superiorRarityButton.addActionListener(this);
		heroicRarityButton.addActionListener(this);
		fabledRarityButton.addActionListener(this);
		
		rarityPanel.add(allRarityButton);
		rarityPanel.add(commonRarityButton);
		rarityPanel.add(superiorRarityButton);
		rarityPanel.add(heroicRarityButton);
		rarityPanel.add(fabledRarityButton);
		
		//########################
		//Type Buttons
		//########################
		JPanel typePanel = new JPanel();
		typePanel.setLayout(new BoxLayout(typePanel, BoxLayout.PAGE_AXIS));
		allTypeButton.setSelected(true);
		allTypeButton.setVisible(true);
		modeSpec1Button.setVisible(true);
		modeSpec2Button.setVisible(true);
		
		switch(mode) {
		case 1: modeSpec1Button.setText("Pots"); modeSpec2Button.setText("Weapons"); 
			recipeMap = AionCrafterApplication.alchemyList.getRecipes(); break;
		case 2: modeSpec1Button.setText("Chain"); modeSpec2Button.setText("Plate"); 
			recipeMap = AionCrafterApplication.armorList.getRecipes(); break;
		case 3: allTypeButton.setVisible(false); modeSpec1Button.setVisible(false); modeSpec2Button.setVisible(false); 
			recipeMap = AionCrafterApplication.cookingList.getRecipes(); break;
		case 4: modeSpec1Button.setText("Jewelry"); modeSpec2Button.setText("Weapons"); 
			recipeMap = AionCrafterApplication.handicraftList.getRecipes(); break;
		case 5: modeSpec1Button.setText("Cloth"); modeSpec2Button.setText("Leather"); 
			recipeMap = AionCrafterApplication.tailorList.getRecipes(); break;
		case 6: allTypeButton.setVisible(false); modeSpec1Button.setVisible(false); modeSpec2Button.setVisible(false); 
			recipeMap = AionCrafterApplication.weaponList.getRecipes(); break;
		}
		
		ButtonGroup group = new ButtonGroup();
		group.add(allTypeButton);
		group.add(modeSpec1Button);
		group.add(modeSpec2Button);
		
		allTypeButton.addActionListener(this);
		modeSpec1Button.addActionListener(this);
		modeSpec2Button.addActionListener(this);
		
		typePanel.add(allTypeButton);
		typePanel.add(modeSpec1Button);
		typePanel.add(modeSpec2Button);
		
		this.add(recipePanel);
		this.add(rarityPanel);
		this.add(typePanel);
	}

	public void actionPerformed(ActionEvent e) {

	}
	
	public void setSubcomponentPanel(SubComponentsPanel scPanel){
		this.scPanel = scPanel;
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if(e.getSource() == recipeTable.getSelectionModel() 
				&& recipeTable.getRowSelectionAllowed()
				&& !e.getValueIsAdjusting()){
			int index = recipeTable.getSelectionModel().getLeadSelectionIndex();
			if(index == -1) return;
			index = recipeTable.convertRowIndexToModel(index);
			if(index == -1) return;
			Recipe recipe = recipeMap.get((String)recipeTable.getModel().getValueAt(index, 2));
			if(recipe != null){
				scPanel.changeRecipe(recipe, 1, recipeMap);
				scPanel.quantity.setText("1");
			}
		}
	}
	
	private void filterTable(){
		RowFilter<RecipeTableModel, Object> filter = null;
		try{
			//(?i) = dirty case-insensitive hack
			filter = RowFilter.regexFilter("(?i)" + searchField.getText(), 1);
		} catch (java.util.regex.PatternSyntaxException ex) {
			return;
		}
		sorter.setRowFilter(filter);
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		if(e.getDocument() == searchField.getDocument()){
			filterTable();
		}
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		if(e.getDocument() == searchField.getDocument()){
			filterTable();
		}
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		if(e.getDocument() == searchField.getDocument()){
			filterTable();
		}
	}

}
